****APPENDIX FIGURE C9****

cd "$main_dir/output/figures/appendix_c"

graph set window fontface "Palatino"

global basic 	sex marital religion edu

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/harmonized_dataset.dta"

merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_rate_mpd_var_exp.dta", nogen keepusing(total_growth_rate_obs wgrowth_rate_1 yob_ln_gdppc)
local varlist bank_crisis_w currency_crisis_w twin_crisis_w
foreach y of local varlist {
	merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/`y'_var_exp.dta", nogen
}

fegen subregion_yob = group(subregion yob) if yob!=-99
fegen svy_survey_year = group(survey_year survey)

cap drop sample
gen sample = 1 if born_country!=0 & total_growth_rate_obs>=0.9*(age-1) & total_growth_rate_obs!=.

//--------------------------------------------------------
//Create Figure C9
//--------------------------------------------------------
							   
matrix coef1 = J(3, 3, .)
	matrix coln coef1 = coefficients lcl ucl
	matrix rown coef1 = "Banking Crises (contraction)" "Currency Crises (contraction)" "Financial Crises (contraction)"

matrix coef2 = J(3, 3, .)
	matrix coln coef2 = coefficients lcl ucl
	matrix rown coef2 = "Banking Crises (contraction)" "Currency Crises (contraction)" "Financial Crises (contraction)"
	
matrix coef3 = J(3, 3, .)
	matrix coln coef3 = coefficients lcl ucl
	matrix rown coef3 = "Banking Crises (contraction)" "Currency Crises (contraction)" "Financial Crises (contraction)"

matrix coef4 = J(3, 3, .)
	matrix coln coef4 = coefficients lcl ucl
	matrix rown coef4 = "Banking Crises (contraction)" "Currency Crises (contraction)" "Financial Crises (contraction)"

local i = 1
local varlist bank_crisis_w currency_crisis_w twin_crisis_w
foreach var of local varlist {
	
	qui reghdfe trust_govt wgrowth_rate_1 w`var'_1 yob_ln_gdppc if sample==1 & total_`var'_obs>=0.9*(age-1) & total_`var'_obs!=., absorb(ccode svy_survey_year subregion_yob age $basic) cluster(ccode)
	
	cap drop z*
	local list trust_govt wgrowth_rate_1 yob_ln_gdppc w`var'_1
	foreach x of local list {
		egen z`x' = std(`x') if e(sample)==1
	}
	
	qui reghdfe ztrust_govt zwgrowth_rate_1 zw`var'_1 zyob_ln_gdppc if e(sample)==1, absorb(ccode svy_survey_year subregion_yob age $basic) cluster(ccode) level(90)
	
	qui mat os = r(table)
	qui mat coef1[`i',1] = os[1,1]
	qui mat coef1[`i',2] = os[5,1]
	qui mat coef1[`i',3] = os[6,1]
	
	qui mat coef2[`i',1] = os[1,2]
	qui mat coef2[`i',2] = os[5,2]
	qui mat coef2[`i',3] = os[6,2]
	
	qui mat coef3[`i',1] = os[1,1]
	qui mat coef3[`i',2] = os[1,1]-1.96*os[2,1]
	qui mat coef3[`i',3] = os[1,1]+1.96*os[2,1]
	
	qui mat coef4[`i',1] = os[1,2]
	qui mat coef4[`i',2] = os[1,2]-1.96*os[2,2]
	qui mat coef4[`i',3] = os[1,2]+1.96*os[2,2]
	
	qui test _b[zwgrowth_rate_1] = _b[zw`var'_1]
	qui scalar p_`var' = r(p)
	qui scalar F_`var' = r(F)
	
	di "`var' done"
	
	local ++i
}

coefplot (matrix(coef1[,1]), ci((coef1[,2] coef1[,3])) offset(-0.05) mcolor(navy) ciopts(color(navy) lwidth(*2)) label(Growth Experience)) ///
		 (matrix(coef3[,1]), ci((coef3[,2] coef3[,3])) offset(-0.05) mcolor(navy) ciopts(color(navy)) label(Growth Experience)) ///
         (matrix(coef2[,1]), ci((coef2[,2] coef2[,3])) offset(0.05) mcolor(maroon) ciopts(color(maroon) lwidth(*2)) label(Other Experience)) ///
	     (matrix(coef4[,1]), ci((coef4[,2] coef4[,3])) offset(0.05) mcolor(maroon) ciopts(color(maroon)) label(Other Experience)), ///
	     graphregion(color(white)) bgcolor(white) xline(0) legend(order(1 "Growth Experience" 5 "Other Experiences") size(small)) xscale(range(-0.1 0.15)) xlabel(-0.1(0.05)0.15, labs(small)) ylabel(,labs(small))
graph export "figure_c9.png", replace
